﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Transactions {
    import Classes.*;

    public class TBuyUpgradeBuildings extends TWorldState {

        protected var m_resources:Array;
        protected var m_resourceIds:Array;
        protected var m_oldItems:Array;
        protected var m_cashSuccess:Boolean = false;

        public function TBuyUpgradeBuildings(_arg1:Array, _arg2:Array, _arg3:int){
            var _local5:MapResource;
            var _local6:Item;
            super(_arg1[0]);
            if ((((_arg3 > 0)) && ((Global.player.cash >= _arg3)))){
                Global.player.cash = (Global.player.cash - _arg3);
                this.m_cashSuccess = true;
            };
            if (!this.m_cashSuccess){
                return;
            };
            this.m_resources = _arg1;
            this.m_resourceIds = new Array();
            this.m_oldItems = new Array();
            var _local4:int;
            while (_local4 < _arg1.length) {
                _local5 = (_arg1[_local4] as MapResource);
                this.m_resourceIds.push(_local5.getId());
                this.m_oldItems.push(_local5.getItem());
                _local6 = Global.gameSettings.getItemByName(_arg2[_local4]);
                _local5.setItem(_local6);
                _local5.setState(_local5.getState());
                _local5.doUpgradeAnimations();
                _local4++;
            };
        }
        override public function perform():void{
            if (this.m_cashSuccess){
                signedWorldAction("buyUpgradeBuildings", this.m_resourceIds);
            };
        }
        override protected function onWorldActionComplete(_arg1:Object):void{
            var _local4:MapResource;
            var _local5:Object;
            var _local2:Array = (_arg1 as Array);
            var _local3:int;
            while (_local3 < this.m_resources.length) {
                _local4 = (this.m_resources[_local3] as MapResource);
                _local5 = _local2[_local3];
                if (((((((!((_local4 == null))) && ((((_local4.getId() == 0)) || ((_local4.getId() >= TEMP_ID_START)))))) && (_local5.hasOwnProperty("id")))) && (!((_local5.id == 0))))){
                    _local4.setId(_local5.id);
                };
                _local3++;
            };
        }
        override protected function onWorldActionFault(_arg1:int, _arg2:String):void{
            var _local4:MapResource;
            var _local3:int;
            while (_local3 < this.m_resources.length) {
                _local4 = (this.m_resources[_local3] as MapResource);
                _local4.setItem(this.m_oldItems[_local3]);
                _local4.setState(_local4.getState());
                _local3++;
            };
        }

    }
}//package Transactions 
